package com.xyz.basesdk.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.xyz.basesdk.customViews.SlideCloseLayout;
import com.xyz.basesdk.helpers.StatusBarHelper;
import com.xyz.basesdk.utils.MyToast;
import com.xyz.basesdk.utils.SpUtil;

/**
 * @author mac
 * on 2018/6/25.
 */
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity {
    public SpUtil sp;
    public boolean isShake = false;
    protected P mPresenter;
    private long lastClickTime = 0;
    private String simpleName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarHelper.setStatusBarLightMode(this);
        if (enableSliding()) {
            SlideCloseLayout rootView = new SlideCloseLayout(this);
            rootView.bindActivity(this);
        }
        sp = SpUtil.getInstance();
        initView(savedInstanceState);
        init();
        initPage();
    }

    protected boolean enableSliding() {
        return true;
    }

    protected void initPage() {
        simpleName = getClass().getSimpleName();
        BaseApplication.addActivity(simpleName, this);
    }

    /**
     * 设置activity layout资源以及执行findViewById的工作
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * View初始化完成后的首先调用的方法,可做数据的初始化,以及默认状态的展示
     */
    protected abstract void init();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.onDestroy();
            mPresenter = null;
        }
        if (BaseApplication.isRemoveActivity) {
            BaseApplication.removeActivity(simpleName);
        }
    }

    /**
     * 跳转不要传递参数的Activity
     *
     * @param clazz 目标Activity Class对象
     */
    public void goActivity(Class clazz) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - lastClickTime > backPressExitTime) {
            startActivity(new Intent(BaseApplication.getContext(), clazz));
        }
        lastClickTime = currentTimeMillis;
    }

    public void goActivityNoWait(Class clazz) {
        startActivity(new Intent(BaseApplication.getContext(), clazz));
    }

    public void goActivity(Intent intent) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - lastClickTime > backPressExitTime) {
            startActivity(intent);
        }
        lastClickTime = currentTimeMillis;
    }

    /**
     * 跳转ActivityForResult
     *
     * @param clazz       Activity class对象
     * @param requestCode 请求码
     */
    public void goActivityForResult(Class clazz, int requestCode) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - lastClickTime > backPressExitTime) {
            startActivityForResult(new Intent(BaseApplication.getContext(), clazz), requestCode);
        }
        lastClickTime = currentTimeMillis;
    }

    public void goActivityForResult(Intent intent, int requestCode) {
        long currentTimeMillis = System.currentTimeMillis();
        int backPressExitTime = 1000;
        if (currentTimeMillis - lastClickTime > backPressExitTime) {
            startActivityForResult(intent, requestCode);
        }
        lastClickTime = currentTimeMillis;
    }

    @Override
    public void onBackPressed() {
        if (isShake) {
            onBackCancel();
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 用于网络取消
     */
    protected abstract void onBackCancel();

    public void error() {

    }

    public void showHint(String msg) {
        MyToast.showToast(msg);
    }

    public void noNet() {

    }

    public void showLoadingFull() {
        isShake = true;
    }

    public void hideLoadingFull() {
        isShake = false;
    }

    public void showLoadingPart() {
        isShake = true;
    }

    public void hideLoadingPart() {
        isShake = false;
    }

}
